<template>
  <div>
    <span class="spanspace"  v-text="labAddress"></span>
    <el-button type="text" icon="el-icon-setting" v-show="is_org_edit" @click="handleUpdate"
    v-hasPermi="['labdata:lbroom:edit']">设置</el-button>

    <!-- 添加或修改实验室地址对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="校区" prop="collegeId">
          <el-select v-model="form.collegeId" size="small" placeholder="请选择校区" v-on:change="changeCollege($event)">
            <el-option
              v-for="dict in this.campusOptOptions"
              :key="dict.id"
              :label="dict.name"
              :value="dict.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="楼宇" prop="buildingId">
          <el-select v-model="form.buildingId" placeholder="请选择楼宇" size="small">
            <el-option
              v-for="dict in this.buildingOptOptions"
              :key="dict.buildingId"
              :label="dict.name"
              :value="dict.buildingId"
            ></el-option>
          </el-select>
        </el-form-item>

        <el-form-item v-for='(item,index) in this.classRoomList' :key="item.key" :label="item.label" :prop="item.room" >
          <el-input v-model="item.room" :placeholder="'请输入'+item.label" size="small" class="room"/>
          <el-button v-if="index+1< classRoomList.length" type="text" @click="roomDel(index)" icon="el-icon-minus"></el-button>
          <el-button v-if="index+1 == classRoomList.length" type="text" @click="roomAdd" icon="el-icon-plus"></el-button>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm" v-hasPermi="['labdata:lbroom:edit']">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listLbroom, addLbroom, updateLbroom,listAllBuilding } from "@/api/labdata/lbroom";

export default {
  name: "LbroomCom",
  data() {
    return {
      labAddress: '无',
      is_org_edit: false,
      // 遮罩层
      loading: true,
      // 实验室地址表格数据
      lbroomList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 校区 dic字典
      campusOptOptions: [],
      // 楼宇 dic字典
      buildingOptOptions: [],
      //楼栋列表
      buildingList:[],
      //房间列表
      classRoomList:[],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        labId: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        collegeId: [
          { required: true, message: "校区不能为空", trigger: "change" }
        ],
        buildingId: [
          { required: true, message: "楼宇不能为空", trigger: "change" }
        ],
      }
    };
  },
  props:{
    labId: Number,
    isEdit: Boolean,
  },
  watch: {
      labId:function(newVal, oldVal){
        this.queryParams.labId = newVal;
        this.form.labId = newVal;
        this.getList();
      },
      isEdit:function(newVal, oldVal){
        this.is_org_edit = newVal;
      }
  },
  created() {
    this.queryParams.labId = this.$props.labId;
    this.form.labId = this.$props.labId;
    this.campusOptOptions = [];
    listAllBuilding().then(response => {
      this.buildingList = response.data;
      let collegeIds = [];
      this.buildingList.forEach(item=>{
        if(!collegeIds.includes(item.collegeName)){
          collegeIds.push(item.collegeName);
          this.campusOptOptions.push({
            id:item.collegeId,
            name:item.collegeName
          });
        }
    });
    });
    this.getList();
  },
  methods: {
    changeCollege(val){
      this.form.collegeId = val;
      this.buildingOptOptions = this.buildingList.filter(item=>{
        if(val == item.collegeId){
          return item;
        }
      });
    },
    /** 查询实验室地址列表 */
    getList() {
      this.loading = true;
      listLbroom(this.queryParams).then(response => {
        this.lbroomList = response.rows;
        if(this.lbroomList.length>0){
          this.form.buildingId = this.lbroomList[0].buildingId;
          for(let i=0;i<this.buildingList.length;i++){
            if(this.buildingList[i].buildingId == this.form.buildingId){
              this.form.collegeId = this.buildingList[i].collegeId;
              this.changeCollege(this.form.collegeId);
              this.labAddress = this.buildingList[i].collegeName+' '+ this.buildingList[i].name;
              break;
            }
          }
          this.classRoomList = [];
          this.lbroomList.forEach((item,index) => {
            this.labAddress = this.labAddress +' '+item.classRoom;
            this.classRoomList.push({
              key: index+1,
              label: '房间号'+(index+1),
              room: item.classRoom,
            });
          });
        }else{
          this.labAddress = '无';
          this.classRoomList = [];
        }
        this.loading = false;
      });
    },
    /** 修改按钮操作 */
    handleUpdate() {
        if(this.classRoomList.length==0){
          this.classRoomList.push({
            key: 1,
            label: '房间号1',
            room: null,
          });
        }
        this.open = true;
        this.title = "设置实验室地址";
    },
    cancel(){
        this.getList();
        this.open = false;
    },
    roomAdd(){
      let no = this.classRoomList[this.classRoomList.length -1].key + 1;
      let addRoom = {
        key: no,
        label: '房间号'+no,
        room: null,
      };
      this.classRoomList.push(addRoom);
    },
    roomDel(index){
      this.classRoomList.splice(index,1);
    },
    /** 提交按钮 */
    submitForm() {
        this.form.addRooms = [];
        this.classRoomList.forEach(item=>{
          if(item.room){
            this.form.addRooms.push(item.room);
          }
        });
        this.form.delRooms = [];
        this.lbroomList.forEach(olditem =>{
          var isdel = true;
          for(let i=0;i<this.form.addRooms.length;i++){
            //console.log('')
            if(olditem.classRoom == this.form.addRooms[i]){
              this.form.addRooms.shift(i,1);
              isdel = false;
              break;
            }
          }
          if(isdel){
            this.form.delRooms.push(olditem.classRoomId);
          }
        });
        addLbroom(this.form).then(response => {
          this.msgSuccess("新增成功");
          this.open = false;
          this.getList();
        });

    },

  }
};
</script>
<style type="text/css" scoped>
  .spanspace {
    margin-right: 5px;
  }
  .room{
    max-width: 200px;
  }
</style>
